package ui.popup 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import ui.button.GCButton;
	import ui.textfield.GCTextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author Roy
	 */
	public class GCNotificationPopup extends Sprite 
	{
		private var MAX_MESSAGE_WIDTH:uint = 170;
		
		private var _textArea:Sprite;
		private var _notificationText:GCTextField;
		private var _yPosition:uint;
		
		private var _mask:Shape;
		
		private var _viewableArea:Sprite;
		private var _scrollUp:GCButton;
		private var _scrollDown:GCButton;
		
		public function GCNotificationPopup() 
		{
			_yPosition = 0;
			_mask = new Shape();
			
			_viewableArea = new Sprite();
			_textArea = new Sprite();
			_scrollUp = new GCButton(onScrollUp);
			_scrollDown = new GCButton(onScrollDown);
			init();
		}
		
		/**
		 * Add a brand new text notification
		 * 
		 * @param	text
		 */
		public function addNotice(text:String, emphasize:Boolean = false):void
		{
			var newNotice:GCTextField = new GCTextField();
			newNotice.autoSize = TextFieldAutoSize.LEFT;
			newNotice.setDimensions(MAX_MESSAGE_WIDTH, 30);
			
			if (emphasize)
				newNotice.textColor = GCConstants.COLOR_RED;
			newNotice.text = text;
			newNotice.y = _yPosition;
			_yPosition += newNotice.height;
			_textArea.addChild(newNotice);
			
			// Scroll this down to the bottom
			_textArea.y -= newNotice.height / 2;
		}
		
		public function get viewableHeight():uint
		{
			return _viewableArea.height;
		}
		
		private function init():void
		{	
			_viewableArea.graphics.beginFill(GCConstants.COLOR_GOLD, 0.6);
			_viewableArea.graphics.drawRoundRect(0, 0, 200, 150, 5);
			_viewableArea.graphics.endFill();
			this.addChild(_viewableArea);
			
			_mask.graphics.beginFill(GCConstants.COLOR_WHITE);
			_mask.graphics.drawRect(0, 0, this.width, this.height);
			_textArea.mask = _mask;
			this.addChild(_mask);
			this.addChild(_textArea);
			
			_scrollUp.loadGraphic(GCResources.up_arrow, 30, 30);
			_scrollDown.loadGraphic(GCResources.down_arrow, 30, 30);
			
			_scrollUp.x = _viewableArea.width - _scrollUp.width;
			_scrollDown.x = _scrollUp.x;
			_scrollUp.y = _viewableArea.y;
			_scrollDown.y = _viewableArea.height - _scrollDown.height;
			_viewableArea.addChild(_scrollUp);
			_viewableArea.addChild(_scrollDown);
			
			this.addEventListener(MouseEvent.CLICK, onClick);
		}
		
		private function onClick(event:MouseEvent):void
		{
			if (event.target == _scrollDown || event.target == _scrollUp)
			{
				return;
			}

			this.visible = false;
		}
		
		private function onScrollUp(event:MouseEvent):void
		{
			_textArea.y += 20;
			
			if (_textArea.y > 0)
			{
				_textArea.y = 0;
			}
		}
		
		private function onScrollDown(event:MouseEvent):void
		{
			if (_textArea.y + _textArea.height > _mask.y + _mask.height)
			{
				_textArea.y -= 20;
			}
		}
	}

}